package handler

import (
	v1 "SmartLibrary/api/v1"
	"SmartLibrary/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type BookAdminHandler struct {
	*Handler
	bookAdminService service.BookAdminService
}

func NewBookAdminHandler(
	handler *Handler,
	bookAdminService service.BookAdminService,
) *BookAdminHandler {
	return &BookAdminHandler{
		Handler:          handler,
		bookAdminService: bookAdminService,
	}
}

// LoginBookAdmin 管理员登陆
func (h *BookAdminHandler) LoginBookAdmin(ctx *gin.Context) {
	//获取参数
	var req v1.BookAdminLoginRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	//服务层代码 service
	token, err := h.bookAdminService.LoginBookAdmin(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BookAdminLoginResponseData{
		AccessToken: token,
	})

}

// GetBookAdmin 返回管理员信息
func (h *BookAdminHandler) GetBookAdmin(ctx *gin.Context) {
	//获取参数
	var req v1.BookAdminInfoRequest
	//改成query获取参数
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	//服务层代码 service
	message, err := h.bookAdminService.GetBookAdmin(ctx, &req.Id)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BookAdminInfoResponse{
		Response: v1.Response{
			Code:    200,
			Message: "管理员信息",
			Data:    message,
		},
	})
}

// BookBorrowBookAdmin 借阅图书
func (h *BookAdminHandler) BookBorrowBookAdmin(ctx *gin.Context) {
	var req v1.BookBorrowRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	//将借阅信息输入其中
	err := h.bookAdminService.BookBorrow(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BookBorrowResponse{})
}

// BookReturnBookAdmin 归还图书(查看是否借出)
func (h *BookAdminHandler) BookReturnBookAdmin(ctx *gin.Context) {
	var req v1.BookReturnRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	//根据图书编号来查询图书是否借出
	err := h.bookAdminService.GetBookMessage(ctx, req.BookNumber)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BookReturnResponse{
		Response: v1.Response{
			Code:    200,
			Message: "图书已借出",
		},
	})
}

// OverdueBookAdmin 逾期信息查询
func (h *BookAdminHandler) OverdueBookAdmin(ctx *gin.Context) {
	var req v1.BookReturnRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	err := h.bookAdminService.GetBookMessage(ctx, req.BookNumber)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	//claims := jwt.MyCustomClaims{
	//	UserId: ctx.Value("claims").(*jwt.MyCustomClaims).UserId,
	//}

	v1.HandleSuccess(ctx, v1.BookReturnResponse{
		Response: v1.Response{
			Code:    200,
			Message: "图书没有逾期",
		},
	})
}

// ReturnBookAdmin 归还图书
func (h *BookAdminHandler) ReturnBookAdmin(ctx *gin.Context) {
	var req v1.BookReturnRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	err := h.bookAdminService.BookReturn(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BookReturnResponse{
		Response: v1.Response{
			Code:    200,
			Message: "归还成功",
		},
	})
}

// BorrowReportBookAdmin 借书报表
func (h *BookAdminHandler) BorrowReportBookAdmin(ctx *gin.Context) {
	var req v1.BorrowReportRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	//
	data, err := h.bookAdminService.BorrowReport(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.BorrowReportResponse{
		Response: v1.Response{
			Code:    200,
			Message: "借书报表",
			Data:    data,
		},
	})
}

// ReturnReportBookAdmin 还书报表
func (h *BookAdminHandler) ReturnReportBookAdmin(ctx *gin.Context) {
	var req v1.ReturnReportRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	data, err := h.bookAdminService.ReturnReport(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.ReturnReportResponse{
		Response: v1.Response{
			Code:    200,
			Message: "还书报表",
			Data:    data,
		},
	})
}

// GetNoticeBookAdmin 获取公告信息
func (h *BookAdminHandler) GetNoticeBookAdmin(ctx *gin.Context) {
	var req v1.GetBookMessageRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	data, err := h.bookAdminService.GetNotice(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.GetBookMessageResponse{
		Response: v1.Response{
			Code:    200,
			Message: "获取公告信息",
			Data:    data,
		},
	})

}

// AddNoticeBookAdmin 添加新公告
func (h *BookAdminHandler) AddNoticeBookAdmin(ctx *gin.Context) {
	var req v1.NoticeRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	err := h.bookAdminService.AddNotice(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.NoticeResponse{
		Response: v1.Response{
			Code:    200,
			Message: "添加新公告成功",
		},
	})
}

// DeleteNoticeBookAdmin 删除公告
func (h *BookAdminHandler) DeleteNoticeBookAdmin(ctx *gin.Context) {
	var req v1.DeleteNoticeRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	err := h.bookAdminService.DeleteNotice(ctx, req.NoticeId)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.NoticeResponse{
		Response: v1.Response{
			Code:    200,
			Message: "删除公告成功",
		},
	})
}

// GetNoticeIdBookAdmin 获取指定公告
func (h *BookAdminHandler) GetNoticeIdBookAdmin(ctx *gin.Context) {
	var req v1.GetNoticeRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	data, err := h.bookAdminService.GetNoticeBookAdmin(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.GetNoticeResponse{
		Response: v1.Response{
			Code:    200,
			Message: "获取指定公告",
			Data:    data,
		},
	})
}

// UpdateNoticeBookAdmin 修改发布的公告
func (h *BookAdminHandler) UpdateNoticeBookAdmin(ctx *gin.Context) {
	var req v1.UpdateNoticeRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrParameterValidation, nil)
		return
	}
	err := h.bookAdminService.UpdateNotice(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.NoticeResponse{
		Response: v1.Response{
			Code:    200,
			Message: "修改公告成功",
		},
	})
}
